package com.anysoft.utils;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import com.anysoft.MentorConst;
import com.anysoft.StaticField;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.UpdateVersionBean;
import com.anysoft.view.dialog.IcbcDialog;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * Created by wwq on 2017/3/26.
 * 版本检测工具类
 */

public class NewVersionUtils {

    private static UpdateVersionBean.DataBean DataBean;
    private static boolean isFurceUpdate;

    public static void checkVersion(final BaseActivity activity, String json) {
        checkVersion(activity, json, true);
    }

    public static void checkVersion(final BaseActivity activity, String json, boolean isShowNoNewVerAlert) {
        final UpdateVersionBean updateInfo;

        try {
            updateInfo = new Gson().fromJson(json, UpdateVersionBean.class);
        } catch (Exception e) {
            Logger.e("VersionUtils", "Gson解析数据时发生异常");
            e.printStackTrace();
            return;
        }
//        String version = updateInfo.getVersion();
        String LocalVersionStr = StaticField.getVersionName(activity);
        String LocalVersion = StringUtils.removePointString(LocalVersionStr);

        DataBean = updateInfo.getData();
        if (DataBean != null) {
            String furceUpdateVersion = DataBean.getFurceUpdateVersion(); //强制更新的版本
            if (!StringUtils.isNullOrEmpty(furceUpdateVersion)
                    && furceUpdateVersion.contains(LocalVersionStr)) {
                isFurceUpdate = true;
            } else {
                isFurceUpdate = false;
            }
            final String lastedVersionStr = DataBean.getLastedVersion();
            String lastedVersion = StringUtils.removePointString(lastedVersionStr);

            if (lastedVersion.compareTo(LocalVersion) > 0) {
                PreferencesUtils.putString(activity, MentorConst.NEW_VERSION_NO, lastedVersionStr);
                IcbcDialog dialog = new IcbcDialog(activity);
                dialog.setAnimationEnable(true);
                dialog.setCancelable(false);
                dialog.setTitle("提示");
//                dialog.setContentText(String.format("发现最新版本V%s,是否去升级？", lastedVersionStr));
                dialog.setContentText(StringUtils.isNullOrEmpty(DataBean.getMessage()) ?
                        String.format("发现最新版本V%s,是否去升级？", lastedVersionStr) :
                        DataBean.getMessage());
                dialog.setEditTextType(false);
                if (!isFurceUpdate) {
                    dialog.setNegativeListener("取消", new IcbcDialog.OnNegativeListener() {
                        @Override
                        public void onClick(IcbcDialog dialog) {
                            dialog.dismiss();
                        }
                    });
                }
                dialog.setPositiveListener("确定", new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
//                        DownLoadHelper.downloadFile(DataBean.getAddress(),DataBean.getMessage());
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(DataBean.getAddress()));
                        PackageManager packageManager = activity.getPackageManager();
                        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
                        boolean isIntentSafe = activities.size() > 0;
                        if (isIntentSafe) {
                            activity.startActivity(intent);
                        }
                        if (!isFurceUpdate) {
                            dialog.dismiss();
                        }
                    }
                }).show();
            } else {
                if (isShowNoNewVerAlert) {
                    activity.showConfirmAlert(activity, "当前版本已经是最新版本");
                }
            }
        }
    }
}
